package com.cuit.freshmanMS.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuit.freshmanMS.VO.StudentVO;
import com.cuit.freshmanMS.domain.Student;
import com.cuit.freshmanMS.mapper.StudentMapper;
import com.cuit.freshmanMS.mapper.StudentViewMapper;
import com.cuit.freshmanMS.query.StudentQuery;
import com.cuit.freshmanMS.service.StudentService;
import com.cuit.freshmanMS.util.StringUtil;
import com.cuit.freshmanMS.util.result.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @description:
 * @author: ziger
 * @time: 2024/1/19 16:10
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    StudentViewMapper studentViewMapper;
    @Override
    public PageResult<StudentVO> pageQuery(StudentQuery studentQuery) {

        // 错误检测
        if (studentQuery.getCurrentPage()==null || studentQuery.getPageSize()==null){
            return new PageResult<>();
        }
        if (studentQuery.getCurrentPage() < 1 || studentQuery.getPageSize() < 1){
            return new PageResult<>();
        }


        Long total = studentViewMapper.count(studentQuery);
        List<StudentVO> students = studentViewMapper.queryPage(studentQuery);
        return new PageResult<>(students, total);
    }

    @Autowired
    StudentMapper studentMapper;

    @Override
    public boolean addstu(Student student) {
        // 插入的数据不能为null
        if(StringUtil.isNull(student.getSex(),student.getStel(),student.getAddress(),student.getSname())){
            return false;
        }
        if (student.getDno() == null || student.getCno() == null || student.getSno() == null || student.getSid() == null){
            return false;
        }

        //身份证号不能重复
        Student studentTemp = new Student();
        studentTemp.setSid(student.getSid());
        boolean idExists = studentMapper.exists(new QueryWrapper<>(studentTemp));

        // 学号不能重复
        studentTemp.setSid(null);
        studentTemp.setSno(student.getSno());
        boolean snoExists = studentMapper.exists(new QueryWrapper<>(studentTemp));

        if (idExists || snoExists){
            return false;
        }

        return studentMapper.addstu(student);
    }

    @Override
    public boolean updatete(Student student) {
        // 数据不能为null
        if(StringUtil.isNull(student.getSex(),student.getStel(),student.getAddress(),student.getSname())){
            return false;
        }
        if (student.getDno() == null || student.getCno() == null || student.getSno() == null || student.getSid() == null){
            return false;
        }
        // 身份证号和学号不能重复
        Student studentTemp = new Student();
        studentTemp.setSid(student.getSid());
        boolean idExists = studentMapper.exists(new QueryWrapper<>(studentTemp));

        if (!idExists){//被修改的学生不存在，直接false
            return false;
        }else {
            Student oldStudent = studentMapper.selectById(student.getSid());
            boolean snoChanged = !Objects.equals(oldStudent.getSno(), student.getSno());
            if (snoChanged){//学号被修改，判断学号是否重复
                studentTemp.setSid(null);
                studentTemp.setSno(student.getSno());
                boolean snoExists = studentMapper.exists(new QueryWrapper<>(studentTemp));
                if (snoExists){//学号重复，返回false
                    return false;
                }
            }
        }

        return studentMapper.updatete(student);
    }
    @Override
    public boolean deleteSno(String sid) {
        // 数据不能为空
        if (StringUtil.isNull(sid)){
            return false;
        }
        // 被删除的学生必须存在
        Student studentTemp = new Student();
        studentTemp.setSid(Integer.valueOf(sid));
        if (!studentMapper.exists(new QueryWrapper<>(studentTemp))){
            return false;
        }
        return studentMapper.deleteSno(sid);
    }

    @Override
    public boolean batchDelete(List<String> sids) {
        return studentMapper.batchDelete(sids);
    }


}
